﻿using NAnt.Core.Attributes;
using NAnt.Core;
using System.Text.RegularExpressions;
using NAnt.SVN.Util;
using NAnt.SVN.Process;

namespace NAnt.SVN.Tasks
{
    /// <summary>
    /// Class to get the version number of the installed SVN executable.
    /// You can run this tasc with the svnversion tag in NAnt.
    /// This value is stored in a parameter to check.
    /// $Id$
    /// </summary>
    [TaskName("svnversion")]
    public sealed class Version : SVNTask
    {
        #region Private Fields

        string requiredversion = string.Empty;
        string regex = @".*(\d+\.\d+\.\d+).*";
        bool firstlineread = false;
        string exeversion = string.Empty;

        #endregion

        #region Public Fields

        /// <summary>
        /// The required SVN version
        /// </summary>
        [TaskAttribute("versionrequired",Required=false)]
        [StringValidator()]
        public string RequiredVersion
        {
            get { return requiredversion; }
            set { requiredversion = value; }
        }

        /// <summary>
        /// Required version
        /// </summary>
        public bool DoCompareRequiredVersion
        {
            get { return !string.IsNullOrEmpty(RequiredVersion); }
        }

        /// <summary>
        /// Get the version
        /// </summary>
        public string ExeVersion
        {
            get { return exeversion; }
        }

        #endregion

        /// <summary>
        /// Initialize command
        /// </summary>
        protected override void InitializeSVNTask()
        {
            firstlineread = false;
            Command = SVNFlags.VERSION;
        }

        /// <summary>
        /// Override the callback
        /// </summary>
        /// <param name="e"></param>
        public override void OnHasRun(ProcessRunnerData e)
        {
            //Only record the first line
            if (!firstlineread)
            {
                Match m = Regex.Match(e.RawOutput, regex, RegexOptions.Multiline & RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    exeversion = m.Groups[1].Value;
                    PropertiesManager.SetSVNProperty(Resources.PropertyNames.ExeVersion, exeversion);
                    if (DoCompareRequiredVersion)
                    {
                        if (!exeversion.Equals(RequiredVersion))
                        {
                            throw new BuildException(
                                string.Format(getString("SVN0311"), RequiredVersion, exeversion));
                        }
                    }
                }
                else
                {
                    if (base.FailOnError) 
                        throw new BuildException(string.Format(getString("SVN0310"), regex));
                    else 
                        Log(Level.Error, string.Format(getString("SVN0310"), regex));
                }
                //Log
                Log(Level.Info, string.Format(getString("SVN0109"), exeversion));
                //Set flag
                firstlineread = true;
            }
            base.OnHasRun(e);
        }
    }
}
